package com.sailorj.medical.web.servlet;

import com.sailorj.medical.domain.area.Area;
import com.sailorj.medical.domain.institution.Institution;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class InstitutionServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String  m=request.getParameter("m");//动作类型参数
        if("list".equals(m)){
            process(request, response, "/page/institution/institution_list.jsp");
        }else if("input".equals(m)){
            process(request, response, "/page/institution/institution_add.jsp");
        }else if("get".equals(m)){
            process(request, response, "/page/institution/institution_edit.jsp");
        }else if("add".equals(m)){
            add(request, response);
            process(request, response, "/page/institution/institution_list.jsp");
        }else if("edit".equals(m)){
            edit(request, response);
            process(request, response, "/page/institution/institution_list.jsp");
        }else if("del".equals(m)){
            del(request, response);
            process(request, response, "/page/institution/institution_list.jsp");
        }else {
            error(request, response);
        }
    }
    /**
     * 添加机构方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void add(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        //接收页面传递过来的参数
        String areacode=request.getParameter("areacode");
        String agencode=request.getParameter("agencode");
        String agenname=request.getParameter("agenname");//有可能出现中文乱码
        //实例化Institution类
        Institution institution =new Institution();
        int grade=areacode.length()==6?1:2;
        //调用添加方法
        try{
            Area area =new Area(areacode);
            institution.addInst(area, agencode, agenname, grade);
        }catch(Exception e){
            error(request, response);
        }

    }

    private void edit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            //接收页面传递过来的参数
            String areacode=request.getParameter("areacode");
            String agencode=request.getParameter("agencode");
            String agenname=request.getParameter("agenname");//有可能出现中文乱码
            Area area = new Area(areacode);
            Institution institution =new Institution(area);
            institution.editInst(agenname,agencode);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    private void del(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 接收行政区域编码+行政区域名称，行政区域名称有可能出现中文乱码
            String areacode = request.getParameter("id");
            Area area = new Area(areacode);
            Institution institution =new Institution(area);
            institution.delInst();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }




}
